package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.Standingbook;
import com.ruoyi.system.mapper.RecordlistMapper;
import com.ruoyi.system.domain.Recordlist;
import com.ruoyi.system.service.IRecordlistService;

/**
 * 养护管理Service业务层处理
 * 
 * @author xjj
 * @date 2022-05-17
 */
@Service
public class RecordlistServiceImpl implements IRecordlistService 
{
    @Autowired
    private RecordlistMapper recordlistMapper;

    /**
     * 查询养护管理
     * 
     * @param recordId 养护管理主键
     * @return 养护管理
     */
    @Override
    public Recordlist selectRecordlistByRecordId(Long recordId)
    {
        return recordlistMapper.selectRecordlistByRecordId(recordId);
    }

    /**
     * 查询养护管理列表
     * 
     * @param recordlist 养护管理
     * @return 养护管理
     */
    @Override
    public List<Recordlist> selectRecordlistList(Recordlist recordlist)
    {
        return recordlistMapper.selectRecordlistList(recordlist);
    }

    /**
     * 新增养护管理
     * 
     * @param recordlist 养护管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordlist(Recordlist recordlist)
    {
        int rows = recordlistMapper.insertRecordlist(recordlist);
        insertStandingbook(recordlist);
        return rows;
    }

    /**
     * 修改养护管理
     * 
     * @param recordlist 养护管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordlist(Recordlist recordlist)
    {
        recordlistMapper.deleteStandingbookByRecordId(recordlist.getRecordId());
        insertStandingbook(recordlist);
        return recordlistMapper.updateRecordlist(recordlist);
    }

    /**
     * 批量删除养护管理
     * 
     * @param recordIds 需要删除的养护管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordlistByRecordIds(Long[] recordIds)
    {
        recordlistMapper.deleteStandingbookByRecordIds(recordIds);
        return recordlistMapper.deleteRecordlistByRecordIds(recordIds);
    }

    /**
     * 删除养护管理信息
     * 
     * @param recordId 养护管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordlistByRecordId(Long recordId)
    {
        recordlistMapper.deleteStandingbookByRecordId(recordId);
        return recordlistMapper.deleteRecordlistByRecordId(recordId);
    }

    /**
     * 新增台账管理信息
     * 
     * @param recordlist 养护管理对象
     */
    public void insertStandingbook(Recordlist recordlist)
    {
        List<Standingbook> standingbookList = recordlist.getStandingbookList();
        Long recordId = recordlist.getRecordId();
        if (StringUtils.isNotNull(standingbookList))
        {
            List<Standingbook> list = new ArrayList<Standingbook>();
            for (Standingbook standingbook : standingbookList)
            {
                standingbook.setRecordId(recordId);
                list.add(standingbook);
            }
            if (list.size() > 0)
            {
                recordlistMapper.batchStandingbook(list);
            }
        }
    }
}
